﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JiaBao.Core.Infrastructure
{
    /// <summary>
    /// 单例
    /// </summary>
    /// <typeparam name="T">对象类型</typeparam>
    public class Singleton<T> : Singleton
    {
        static T _instance;

        /// <summary>
        /// 指定类型<see cref="T">的单例
        /// </summary>
        public static T Instance
        {
            get { return _instance; }
            set
            {
                _instance = value;
                AllSingletons[typeof(T)] = value;
            }
        }
    }


    /// <summary>
    /// 为<see cref="IList{T}"> 类型提供单例
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SingletonList<T> : Singleton<IList<T>>
    {
        static SingletonList()
        {
            Singleton<IList<T>>.Instance = new List<T>();
        }

        /// <summary>
        /// 指定类型 T <see cref="IList{T}"> 的单例
        /// </summary>
        public new static IList<T> Instance
        {
            get { return Singleton<IList<T>>.Instance; }
        }

    }

    /// <summary>
    ///  为<see cref="IDictionary{TKey, TValue}"> 类型提供单例
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    public class SingletonDictionary<TKey, TValue> : Singleton<IDictionary<TKey, TValue>>
    {
        static SingletonDictionary()
        {
            Singleton<Dictionary<TKey, TValue>>.Instance = new Dictionary<TKey, TValue>();
        }

        /// <summary>
        /// 指定类型T <see cref="IDictionary{TKey, TValue} "> 的单例
        /// </summary>
        public new static IDictionary<TKey, TValue> Instance
        {
            get { return Singleton<Dictionary<TKey, TValue>>.Instance; }
        }
    }

    /// <summary>
    /// 对<see cref="Singleton{T}"> 提供底层服务
    /// </summary>
    public class Singleton
    {
        static readonly IDictionary<Type, object> _allSingletons;
        static Singleton()
        {
            _allSingletons = new Dictionary<Type, object>();
        }



        /// <summary>
        /// 单例字典
        /// </summary>
        public static IDictionary<Type, object> AllSingletons
        {
            get { return _allSingletons; }
        }
    }
}
